<?php

/**
 * GradeTurma form.
 *
 * @package    form
 * @subpackage GradeTurma
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class GradeTurmaForm extends BaseGradeTurmaForm
{
  	public function configure()
  	{
  		parent::configure();
		
  		
  		$form_hora_inicio 	= new sfForm();
  		$form_hora_fim 		= new sfForm();
  		$form_disciplina 	= new sfForm();
  		$form_docente 		= new sfForm();
  		$form_id_disciplina 	= new sfForm();
  		$form_id_docente 		= new sfForm();
  		
		for ($i = 0; $i < 4; $i++) {
		 $form_hora_inicio->widgetSchema[$i] 	= new sfWidgetFormInput( array(), array('class'=> 'span1 hora'));
		 $form_hora_inicio->setValidator($i, new sfHourValidator(array('required' => false)));
				
		 $form_hora_fim->widgetSchema[$i] 		= new sfWidgetFormInput( array(), array('class'=> 'span1 hora'));
		 $form_hora_fim->setValidator($i, new sfHourValidator(array('required' => false)));
		 
		 $form_disciplina->widgetSchema[$i] 	= new sfWidgetFormInput(array(), array('class'=> 'span2 disciplina'));
		 
		 $form_docente->widgetSchema[$i] 		= new sfWidgetFormInput(array(), array('class'=> 'span2 docente'));
		 
		 $form_id_docente->widgetSchema[$i] 	= new sfWidgetFormInputHidden();
		 $form_id_docente->setValidator($i, new sfValidatorInteger(array('required' => false)));
		 
		 $form_id_disciplina->widgetSchema[$i] 	= new sfWidgetFormInputHidden();
		 $form_id_disciplina->setValidator($i, new sfValidatorInteger(array('required' => false)));
		}
		
		$form_hora_inicio->validatorSchema->setOption('allow_extra_fields', true);
		$form_hora_fim->validatorSchema->setOption('allow_extra_fields', true);
		$form_disciplina->validatorSchema->setOption('allow_extra_fields', true);
		$form_docente->validatorSchema->setOption('allow_extra_fields', true);
		$form_id_docente->validatorSchema->setOption('allow_extra_fields', true);
		$form_id_disciplina->validatorSchema->setOption('allow_extra_fields', true);
  		
		
		$this->embedForm('form_hora_inicio', 	$form_hora_inicio);
		$this->embedForm('form_hora_fim', 		$form_hora_fim);
		$this->embedForm('form_disciplina', 	$form_disciplina);
		$this->embedForm('form_docente', 		$form_docente);
		$this->embedForm('form_id_disciplina', 	$form_id_disciplina);
		$this->embedForm('form_id_docente', 	$form_id_docente);

  		$this->widgetSchema->setLabels(array(
  		      	'id_grade_turma' => 'IdGradeTurma',
		      	'id_turma'       => 'IdTurma',
		      	'dt_grade'       => 'DtGrade',
		      	'nm_professor'   => 'NmProfessor',
		      	'nu_hora_inicio' => 'NuHoraInicio',
		      	'nu_hora_fim'    => 'NuHoraFim',
		      	'ds_disciplina'  => 'DsDisciplina',
		      	'created_at'     => 'CreatedAt',
		      	'updated_at'     => 'UpdatedAt',
				));
				
		$this->validatorSchema->setOption('allow_extra_fields', true);
		unset($this['nu_hora_inicio'], $this['id_grade_turma'], $this['id_disciplina'], $this['nu_hora_fim'], $this['id_docente']);
  	}


	public function doSave($con = null)
	{
		if(count($this->getValue('form_hora_inicio'))
			&& count($this->getValue('form_hora_fim'))
			&& count($this->getValue('form_id_docente'))
			&& count($this->getValue('form_id_disciplina')))
		{
			$form_hora_inicio	= $this->getValue('form_hora_inicio');
			$form_hora_fim		= $this->getValue('form_hora_fim');
			$form_id_disciplina	= $this->getValue('form_id_disciplina');
			$form_id_docente	= $this->getValue('form_id_docente');

			for($i=0; $i<4; $i++)
			{
				if($form_hora_inicio[$i] != ''
					&& $form_hora_fim[$i] != ''
					&& $form_id_disciplina[$i] != ''
					&& $form_id_docente[$i] != '')
				{
					$this->getObject()->setNuHoraInicio($form_hora_inicio[$i]);
					$this->getObject()->setNuHoraFim($form_hora_fim[$i]);
					$this->getObject()->setNuDiaSemana($this->getValue('nu_dia_semana'));
					$this->getObject()->setIdDisciplina($form_id_disciplina[$i]);
					$this->getObject()->setIdDocente($form_id_docente[$i]);
					$this->getObject()->setIdTurmaAnoLetivo($this->getValue('id_turma_ano_letivo'));
					$this->getObject()->save();
					//parent::doSave($con);
				}
				/*
				$grade   = ($this->getValue('id_grade_turma')) ? Doctrine::getTable('GradeTurma')->find($this->getValue('id_grade_turma')) : new GradeTurma();
				$grade->setNuHoraInicio($this->getValue('username'));
				$grade->setNuHoraFim($this->getValue('password'));
				$grade->setDia(true);
				$grade->setIdDisciplina(true);
				$grade->setIdDocente(true);
				$grade->save();
				*/
			}
		}
		else
			throw new Exception('Erro no formulário');
	}

	public function updateObject($values = null)
	{
		$object = parent::updateObject($values);
		//$object->setUserId($this->UserId);

		// Don't break subclasses!
		return $object;
	}
}